package com.goingup.simulation;

import java.util.ArrayList;
import java.util.List;

import com.goingup.domain.Building;
import com.goingup.domain.Passenger;

public class Simulator {
	private TripFactory tripFactory;
	private List<Passenger> goingUpPassengers;
	private List<Passenger> goingDownPassengers;
	private boolean done =false;

	public Simulator()
	{
		Building building = new Building(10);
		tripFactory = new TripFactory(building);
		List<Passenger> passengers = new ArrayList<Passenger>();
		for (int i = 0; i < 1000; i++) {
			Passenger passenger = new Passenger();
			building.getGroundFloor().addPassenger(passenger);
			passengers.add(passenger);
		}
		goingUpPassengers = new ArrayList<Passenger>();
		goingDownPassengers = new ArrayList<Passenger>();
		goingUpPassengers.addAll(passengers);
	}
	public void start()
	{
		new Thread(new Clock(this)).start();
		while(!done ){}
		System.out.print("Done simulation");
	}
	protected synchronized void newArrival()
	{
		System.out.print("New Arrival \n");
		if (!goingUpPassengers.isEmpty())
		{
			Passenger nextPassenger = goingUpPassengers.remove(0);
			goingDownPassengers.add(nextPassenger);
			tripFactory.initializeTrip(nextPassenger);
		}else if (!goingDownPassengers.isEmpty())
		{
			Passenger nextPassenger = goingDownPassengers.remove(0);
			tripFactory.initializeTrip(nextPassenger);
		}
		else 
		{
			done=true;
		}
	}
	public static void main(String[] args)
	{
		Simulator simulator = new Simulator();
		simulator.start();
	}
}
